<?php
session_start();
require("../bin/common.php");
checkSess();
checkInstall();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Add Patch - Vanadium Patch Mananger</title>
<link href="../lib/style.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="../lib/script.js"></script>
</head>
<body onload="fixSize()" onresize="fixSize()">

<div class="container" id="container">

	<!-- Header -->
	<div class="top">
	Vanadium Patch Manager

	<?php
	dispLogin();
	?>
	</div>
    
	<!-- Left Hand Panel -->
	<div class="left">
		<br /><span class="nav_option"><img src="../lib/gfx/icon_home.gif" width="15" height="15" alt="Home" /> <a href="../home/">Home</a></span><br /><br />
		<span class="nav_tab_option"><img src="../lib/gfx/icon_add.gif" width="12" height="12" alt="Add" /> <a href="../addpatch/">Add Patch</a></span><br /><br />
		<span class="nav_tab_option"><img src="../lib/gfx/icon_edit.gif" width="13" height="16" alt="Edit" /> <a href="../patch/changelog.php">View Changelog</a></span><br /><br />
		<span class="nav_tab_option"><img src="../lib/gfx/icon_edit.gif" width="13" height="16" alt="Edit" /> <a href="../editpatch/">Edit Patch Home</a></span><br /><br />
		<span class="nav_tab_option"><img src="../lib/gfx/icon_setup.gif" width="14" height="16" alt="Setup" /> <a href="../setup/">Setup</a></span><br /><br />
        <span class="nav_tab_tab_option"><img src="../lib/gfx/icon_edit.gif" width="13" height="16" alt="Edit" /> <a href="../setup/neoncube.php">Neoncube.ini</a></span><br /><br />
        <span class="nav_tab_tab_option"><img src="../lib/gfx/icon_account.gif" width="14" height="14" alt="Accounts" /> <a href="../setup/accounts.php">Accounts</a></span><br /><br />
        <span class="nav_tab_tab_option"><img src="../lib/gfx/icon_cp.gif" width="16" height="16" alt="Control Panel" /> <a href="../setup/cpanel.php">Control Panel</a></span>
	</div>
    
	<!-- Body Content Container -->
	<div class="content_container" id="content_container">
    
		<!--Content Title -->
		<div class="content_title">
			Add Patch - Result
		</div>
        
		<!--Content Body -->
		<div class="content_body">
			<?php
			//define a maxim size for the uploaded images in Kb
			define ("MAX_SIZE","1000000");
			//This function reads the extension of the file. It is used to determine if the file is an image by checking the extension.
			function getExtension($str) {
				$i = strrpos($str,".");
				if (!$i) { return ""; }
				$l = strlen($str) - $i;
				$ext = substr($str,$i+1,$l);
				return $ext;
			}
			//This variable is used as a flag. The value is initialized with 0 (meaning no error found) and it will be changed to 1 if an errro occures. If the error occures the file will not be uploaded
			$errors=0;
			//checks if the form has been submitted
			if(isset($_POST['Submit'])) {
				//reads the name of the file the user submitted for uploading
				$image=$_FILES['patch']['name'];
				//if it is not empty
				if ($image) {
					//get the original name of the file from the clients machine
					$filename = stripslashes($_FILES['patch']['name']);
					//get the extension of the file in a lower case format
					$extension = getExtension($filename);
					$extension = strtolower($extension);
					//if it is not a known extension, we will suppose it is an error and will not upload the file, otherwize we will do more tests
					if (($extension != "rar") && ($extension != "gpf"))
					{
						//print error message
						echo '<b>Error:</b> The file extention is not valid. It must be a .rar file or a .gpf file.';
						$errors=1;
						}
						else
						{
							//get the size of the image in bytes
							//$_FILES['image']['tmp_name'] is the temporary filename of the file in which the uploaded file was stored on the server 
							$size=filesize($_FILES['patch']['tmp_name']);
							//compare the size with the maxim size we defined and print error if bigger
							if ($size > MAX_SIZE*1024)
							{
								echo '<b>Error:</b> You have exceeded the maximum file limit of 1GB. Nobody will want to patch anything that large, or even remotley close to 50MB.';
								$errors=1;
								}
								//we will give an unique name, for example the time in unix time format
								$the_time = time();
								$image_name=$the_time.'.'.$extension;
								//the new name will be containing the full path where will be stored (images folder)
								$newname="../patch/".$image_name;
								//we verify if the image has been uploaded, and print error instead
								$copied = copy($_FILES['patch']['tmp_name'], $newname);
								if (!$copied)
								{
									echo '<b>Error:</b> Server Error, the file did not copy successfully. Please try upload again.';
									$errors=1;
									}}}}
									//If no errors registred, print the success message
									if(isset($_POST['Submit']) && !$errors) {
										
										$changelog = $_POST['desc'];
										
										//Neaten up file ext.
										switch($extension) {
											case "rar":
												$upfilext = "RAR Archive";
												$patch_ext = "FLD";
											break;
											case "gpf":
												$upfilext = "GPF File";
												$patch_ext = "GPF";
											break;
										}
										
										//Find Current Patch Number
										$patchdata = file("../lib/patchdata.txt");
										
										foreach($patchdata as $key => $val) 
										{
										   //explode that data into a new array:  
										   $data[$key] = explode("||", $val);
										}
										
										$patchno = 99;
										for($k = 0; $k < sizeof($patchdata); $k++) 
										{
											if ($data[$k][0] > $patchno) {
												$patchno = $data[$k][0];
											}
										}
										//Add 1 to patchno
										$patchno++;
										
										//Insert new data for changelog
										$fp = fopen("../lib/patchdata.txt","a");
										
										if(!$fp) {
											echo 'Error: Cannot open changelog file.';
											exit;
										}
	
										//Check if any entires exist in changelog
										$change_file = "../lib/patchdata.txt"; 

										$change_lines = count(file($change_file));
										
										if ($change_lines == 0) {
											fwrite($fp, "".$patchno."||".$the_time."||".$_SESSION['logged_in']."||".$changelog."");
										}
										else {
											fwrite($fp, "\n".$patchno."||".$the_time."||".$_SESSION['logged_in']."||".$changelog."");
										}
										
										fclose($fp);
										
										//Add it to patchlist.txt
										$fp = fopen("../patch/patchlist.txt","a");
										
										if(!$fp) {
											echo 'Error: Cannot open patchlist.txt file.';
											exit;
										}
										
										//Check if any entires exist in patchlist.txt
										$file = "../patch/patchlist.txt"; 

										$lines = count(file($file));
										
										if ($lines == 0) {
											fwrite($fp, "".$patchno."	".$patch_ext."	".$image_name."");
										}
										else {
											fwrite($fp, "\n".$patchno."	".$patch_ext."	".$image_name."");
										}
										
										fclose($fp);

										//Report on update
										echo "<b>Success</b>: Your file has been uploaded.<br /><br />
										<b>File Name:</b> ".$image_name."<br />
										<b>Type:</b> ".$upfilext."<br />
										<b>Patch No.:</b> ".$patchno."<br />
										<b>Comments:</b> ".$changelog."";
									}

			?>
		</div>
    
	<!--End Content Container-->
	</div>
    
	<!--Footer -->
	<div class="footer">
		Vanadium Patch Manager, A Neoncube Patch Modification.<br />
		GNU General Public License v3 - 2010<br />
		http://code.google.com/p/vanadiumpatch/
	</div>

<!--End Container-->
</div>
</body>
</html>